// 点击播放
$('#ku-play').on('click', function () {
    getPlay(location.search.split('=')[1])
})


// 点击播放函数
function getPlay(id) {
    fetch(replaceHost(`http://www.kuwo.cn/api/www/music/musicInfo?mid=${id}&httpsStatus=1&reqId=9d16f3f0-e7fe-11ed-981a-6d69b5040abe`)).then(res => res.json()).then((data) => {
        fetch(replaceHost(`http://www.kuwo.cn/api/v1/www/music/playUrl?mid=${id}&type=music&httpsStatus=1&reqId=9d16f3f1-e7fe-11ed-981a-6d69b5040abe`)).then(res => res.json()).then((src) => {
            if (src.code === 200) {
                setLocalStorage(data.data)
                musicPlay(data.data, src.data.url)
            } else {
                alert('无法播放')
            }
        })
    })
}

// 保存本地
function setLocalStorage(data) {
    if (!localStorage.getItem('musicData')) {
        let musicData = [data]
        localStorage.setItem('musicData', JSON.stringify(musicData))
    } else {
        let musicData = JSON.parse(localStorage.getItem('musicData'))
        if (!musicData.find((v, i) => {
            return v.albumid === data.albumid
        })) {
            musicData.unshift(data)
            localStorage.setItem('musicData', JSON.stringify(musicData))
        }
    }
}

// 默认播放第一个
let index = 0

window.onload = function () {
    musicIndex(index, false)
}

// 播放
function musicIndex(index, isBool = true) {
    if (localStorage.getItem('musicData')) {
        let musicData = JSON.parse(localStorage.getItem('musicData'))
        fetch(replaceHost(`http://www.kuwo.cn/api/v1/www/music/playUrl?mid=${musicData[index].rid}&type=music&httpsStatus=1&reqId=9d16f3f1-e7fe-11ed-981a-6d69b5040abe`)).then(res => res.json()).then((src) => {
            musicPlay(musicData[index], src.data.url, isBool)
        })
    }
}

// 渲染数据
function musicPlay(data, src, isBool = true) {
    $('#music-pic').attr('src', data.albumpic)
    $('#music-name').html(data.name + '-' + data.artist)
    $('.music')[0].src = src
    $('#music-date').html(`${musicDate($('.music')[0].currentTime)}/${data.songTimeMinutes}`)

    // 播放
    if (isBool) {
        $('#play').children().removeClass('icon-bofang-01')
        $('#play').children().addClass('icon-bofang1')
        $('.music')[0].play()
    }
}


// 播放
$('#play').on('click', function () {
    if ($(this).children().hasClass('icon-bofang-01')) {
        $(this).children().removeClass('icon-bofang-01')
        $(this).children().addClass('icon-bofang1')
        // 播放
        $('.music')[0].play()
    } else {
        $(this).children().removeClass('icon-bofang1')
        $(this).children().addClass('icon-bofang-01')
        // 暂停
        $('.music')[0].pause()
    }
})

// 播放进度
$('.music').on('timeupdate', function () {
    if ($(this)[0].currentTime > 0) {
        $('#music-date').html(`${musicDate($(this)[0].currentTime)}/${musicDate($(this)[0].duration)}`)
        $('#progress').val(parseInt($(this)[0].currentTime) / (parseInt($(this)[0].duration) / 100))
    }

    if (!$('.music')[0].loop) {
        if (parseInt($(this)[0].currentTime) / (parseInt($(this)[0].duration) / 100) === 100) {
            $('#next').trigger('click')
        }
    }

})

// 音乐进度
$('#progress').on('change', function () {
    $('.music')[0].currentTime = $(this).val() * parseInt($('.music')[0].duration) / 100
})

$('.music')[0].volume = $('#volume').val() / 100
$('#volume').on('change', function () {
    $('.music')[0].volume = $(this).val() / 100
})

// 循环播放
$('.icon').children().eq(3).on('click', function () {
    if ($(this).hasClass('icon-24gl-repeat2')) {
        $(this).removeClass('icon-24gl-repeat2')
        $(this).addClass('icon-24gl-repeatOnce2')
        $('.music')[0].loop = true
    } else {
        $(this).removeClass('icon-24gl-repeatOnce2')
        $(this).addClass('icon-24gl-repeat2')
        $('.music')[0].loop = false
    }
})


// 格式化音乐时间
function musicDate(num) {
    let f = '0' + parseInt(num / 60)
    let s = num % 60 < 10 ? '0' + parseInt(num % 60) : parseInt(num % 60)
    return f + ':' + s
}


// 上一个
$('#pre').on('click', function () {
    let musicData = JSON.parse(localStorage.getItem('musicData'))
    index--
    if (index < 0) {
        index = musicData.length - 1
        musicIndex(index)
    } else {
        musicIndex(index)
    }
})


// 下一个
$('#next').on('click', function () {
    let musicData = JSON.parse(localStorage.getItem('musicData'))
    index++
    if (index === musicData.length) {
        index = 0
        musicIndex(index)
    } else {
        musicIndex(index)
    }
})

$('.islook').on('click', function () {
    if ($(this).children().hasClass('icon-24gl-unlock4')) {
        $(this).children().removeClass('icon-24gl-unlock4')
        $(this).children().addClass('icon-24gl-lock2')
        $('.ku-playControl').css('bottom', '0')
    } else {
        $(this).children().removeClass('icon-24gl-lock2')
        $(this).children().addClass('icon-24gl-unlock4')
        $('.ku-playControl').css('bottom', '-72px')
    }
})